/*
	百度图片
		地址：https://image.baidu.com/search/acjson?
			经过分析 为 GET请求 且绝大多数的参数都是在请求头上 还需要cookie和User-Agent
*/

const axios = require('axios')
const qs = require('qs')

class getImgSlation {
	constructor(baseUrl) {
		this.baseUrl = baseUrl
		this.cookies = ''
		this.params = {}
	}

	init(queryString) {
		this.cookies =
			'BDqhfp=%E7%8B%97%26%26-10-1undefined%26%261161%26%263; BIDUPSID=0926FEA3F19B8984D63F6C2EFF84EF22; PSTM=1605612743; BAIDUID=0926FEA3F19B89841ACC26F514170E64:FG=1; BDUSS=Y2ZnpnLTZmaE8tejdVTExGWnl1N1E2RXdHU3lHYmtsaVlhV1hhLXE3U0F6ZHhmRVFBQUFBJCQAAAAAAAAAAAEAAAA8ciXRSmltbXloYW8xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIBAtV-AQLVfUG; BDUSS_BFESS=Y2ZnpnLTZmaE8tejdVTExGWnl1N1E2RXdHU3lHYmtsaVlhV1hhLXE3U0F6ZHhmRVFBQUFBJCQAAAAAAAAAAAEAAAA8ciXRSmltbXloYW8xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIBAtV-AQLVfUG; __yjs_duid=1_c62cedb10346e52e1a4a6513443fa99f1618714380168; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=33821_31254_33848_33607_26350_33811; cleanHistoryStatus=0; BDSFRCVID=u0AOJeC62iMBvEce7uTmwRecCjYrBp5TH6aokSRUCfyo2ZG8-0T2EG0Pox8g0Kubqm_IogKK3mOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRk8oI0aJDvDqTrP-trf5DCShUFsat7RB2Q-XPoO3K8WVh-RbjJCjx_W0xo23-QiW5cpoMbgylRM8P3y0bb2DUA1y4vpWqIt2gTxoUJ2abj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ZGZkMTRjZjE1NjdhNGEyNjhlOGY2ZWYwMWYzZjdjZWE4Mjk1MDdjYTdmY2JhZmQ4Nzg1ZTcwMDk0OGI0YjJmYjUzZTlhZWI5ZDM0MTI4ZmNmMWU1NThmODY1N2FjZmU4YzM2MzA1ODU3OWU4ZTE2NWE1ZWUwYTk0ZjUxMTJkNTE='
		this.params = {
			tn: 'resultjson_com',
			logid: '11420582710512006039',
			ipn: 'rj',
			ct: '201326592',
			is: '',
			fp: 'result',
			queryWord: queryString,
			cl: '2',
			lm: '-1',
			ie: 'utf-8',
			oe: 'utf-8',
			adpicid: '',
			st: '-1',
			z: '',
			ic: '',
			hd: '',
			latest: '',
			copyright: '',
			word: queryString,
			s: '',
			se: '',
			tab: '',
			width: '',
			height: '',
			face: '0',
			istype: '2',
			qc: '',
			nc: '1',
			fr: '',
			expermode: '',
			force: '',
			pn: '1', // 从哪里开始拿
			rn: '30', // 一次拿多少张
			gsm: '5a',
		}
		this.params['time'] = new Date().getTime()
	}

	getImgs() {
		axios.default.interceptors.request.use(config => {
			config.headers['Cookie'] = this.cookies
			config.headers['User-Agent'] =
				'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
			console.log(config)
			return config
		})
		return axios.get(this.baseUrl + qs.stringify(this.params))
	}
}

const Slader = new getImgSlation('https://image.baidu.com/search/acjson?')

Slader.init('猫')

Slader.getImgs().then(res => {
	console.log('xxx', res.data)
})
